package com.aspose.utils;


import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;


/**
 * Word或Excel 转Pdf 帮助类
 *
 * @author lenovo
 * 备注:需要引入 aspose-words-15.8.0-jdk16.jar / aspose-cells-8.5.2.jar
 */
public class documentUtils {
    private  static String license = "<License>" +
            "  <Data>" +
            "    <Products>" +
            "      <Product>Aspose.Total for Java</Product>" +
            "    </Products>" +
            "    <EditionType>Enterprise</EditionType>" +
            "    <SubscriptionExpiry>20991231</SubscriptionExpiry>" +
            "    <LicenseExpiry>20991231</LicenseExpiry>" +
            "    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>" +
            "  </Data>" +
            "  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>" +
            "</License>";
    private static boolean getWordLicense() {
        boolean result = false;
        try {
            InputStream is = getStringStream(license);
            com.aspose.words.License aposeLic = new  com.aspose.words.License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    private static boolean getExcelLicense() {
        boolean result = false;
        try {
            InputStream is = getStringStream(license);
            com.aspose.cells.License aposeLic = new  com.aspose.cells.License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    private static boolean getPPTLicense() {
        boolean result = false;
        try {
            InputStream is = getStringStream(license);
            com.aspose.slides.License aposeLic = new  com.aspose.slides.License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * @param wordPath 需要被转换的word全路径带文件名
     * @param pdfPath  转换之后pdf的全路径带文件名
     */
    public static void doc2pdf(String wordPath, String pdfPath) {
        if (!getWordLicense()) {
            return;
        }
        try {
            long old = System.currentTimeMillis();
            File file = new File(pdfPath);
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(wordPath);
            doc.save(os, com.aspose.words.SaveFormat.PDF);
            long now = System.currentTimeMillis();
            os.close();
            System.out.println("共耗时：" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param excelPath 需要被转换的excel全路径带文件名
     * @param pdfPath   转换之后pdf的全路径带文件名
     */
    public static void excel2pdf(String excelPath, String pdfPath) {
        if (!getExcelLicense()) {
            return;
        }
        try {
            long old = System.currentTimeMillis();
            Workbook wb = new Workbook(excelPath);
            FileOutputStream fileOS = new FileOutputStream(new File(pdfPath));
            wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);
            fileOS.close();
            long now = System.currentTimeMillis();
            System.out.println("共耗时：" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static void ppt2pdf(String from, String to) {
        // 验证License
        if (!getPPTLicense()) {
            return;
        }
        try {
            long old = System.currentTimeMillis();
            File file = new File(to);
            Presentation pres = new Presentation(from);
            FileOutputStream fileOS = new FileOutputStream(file);
            pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf);
            fileOS.close();
            long now = System.currentTimeMillis();
            System.out.println("共耗时：" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static InputStream getStringStream(String sInputString) {
        if (sInputString != null && !sInputString.trim().equals("")) {
            try {
                ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(sInputString.getBytes());
                return tInputStringStream;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }

    public static void main(String[] args) {

        //word 和excel 转为pdf
        String filePaths = "/Users/chenyuemei/Desktop/question.xls";
        String fileName = System.currentTimeMillis()+"_";
        String pdfPath = "/Users/chenyuemei/Desktop/" + fileName + ".pdf";
        excel2pdf(filePaths, pdfPath);

        filePaths = "/Users/chenyuemei/Desktop/VR安全培训设计说明书.docx";
        fileName = System.currentTimeMillis()+"_";
        pdfPath = "/Users/chenyuemei/Desktop/" + fileName + ".pdf";
        doc2pdf(filePaths, pdfPath);

        filePaths = "/Users/chenyuemei/Desktop/分布式思维.pptx";
        fileName = System.currentTimeMillis()+"_";
        pdfPath = "/Users/chenyuemei/Desktop/" + fileName + ".pdf";
        ppt2pdf(filePaths, pdfPath);

    }
}